<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="/resources/template/template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:stores="http://java.sun.com/jsf/composite/store">

    <ui:define name="content">

        <h:outputStylesheet library="css" name="maincss.css" ></h:outputStylesheet>

        <h:form id="bill">
            <p:panel rendered="#{!storePurchaseController.printPreview}">
                
                <p:panel>

                    <f:facet name="header" > 
                        <h:panelGrid columns="8"  >
                            <h:outputText value="Store Purchase" />                     
                            <p:selectOneMenu   id="cmbPs" value="#{storePurchaseController.bill.paymentMethod}">                                                                     
                                <f:selectItems value="#{enumController.paymentMethodsForPo}" />
                            </p:selectOneMenu>
                            <p:outputLabel value="Select Dealor"/>
                            <p:autoComplete converter="deal" value="#{storePurchaseController.bill.fromInstitution}" 
                                            forceSelection="true"
                                            completeMethod="#{storeDealorController.completeDealor}" var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" />
                            <p:commandButton  value="Settle" action="#{storePurchaseController.settle}" ajax="false"  style="width: 150px; padding: 1px;border: 1px solid ; margin: auto;">
                            </p:commandButton>
                            <p:commandButton value="New Bill" ajax="false" action="#{storePurchaseController.makeNull()}" >
                            </p:commandButton>

                        </h:panelGrid>

                    </f:facet>
                </p:panel>


                <p:commandButton id="nullButton" value="No Action" action="#" style="display: none;" ></p:commandButton>

                <p:defaultCommand  target="btnAdd" />  

                <h:panelGrid id="itemselectgrid" columns="1" style="min-width: 100%;">
                    <p:focus id="focusItem" for="acItem" ></p:focus>
                    
                    <p:panelGrid id="itemSelectgrid1" columns="8">                       
                        <h:outputLabel value="Select Item"/>
                        <h:outputLabel value="Qty"/>
                        <h:outputLabel value="Free"/>
                        <h:outputLabel value="Purchase Rate"></h:outputLabel>
                        <h:outputLabel value="Sale Price"></h:outputLabel>
                        <h:outputLabel value="DOE(dd mm yy)/Warrenty Date"/>
                        <h:outputLabel value="Bathch No"/>
                        <h:outputLabel ></h:outputLabel>

                        <p:autoComplete id="acItem"   value="#{storePurchaseController.currentBillItem.item}" 
                                        forceSelection="true"
                                        completeMethod="#{itemController.completeStoreItem}" 
                                        var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" >
                            <p:column headerText="Item" >
                                <h:outputLabel value="#{vt.name}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Code" >
                                <h:outputLabel value="#{vt.code}"></h:outputLabel>
                            </p:column>
                            <f:ajax event="itemSelect" execute="@this" render="txtInventoryCode panelInventoryItemDetails" listener="#{storePurchaseController.createSerialNumber()}" ></f:ajax>
                        </p:autoComplete>
                        <p:inputText autocomplete="off" id="txtQty" styleClass="numericTxt" value="#{storePurchaseController.currentBillItem.tmpQty}" style="width:100%" />  
                        <p:inputText autocomplete="off" id="txtFreeQty"  styleClass="numericTxt" 
                                     value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.freeQty}" style="width:100%" /> 
                        <p:inputText autocomplete="off" id="txtPrate"  styleClass="numericTxt" 
                                     value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.purchaseRate}"  >
                            <f:ajax event="blur" execute="@this" render="tmpRetail" 
                                    listener="#{storePurchaseController.purchaseRateListener(storePurchaseController.currentBillItem.pharmaceuticalBillItem)}"/>
                        </p:inputText>
                        <p:inputText autocomplete="off" id="tmpRetail"  styleClass="numericTxt" 
                                     value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.retailRate}"  />  
                        <p:calendar navigator="true"
                                    id="calDoe" pattern="dd MM yy" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.doe}"   >
                            <f:ajax event="dateSelect" execute="@this" render="tmp" listener="#{storePurchaseController.setBatch()}"/>
                        </p:calendar> 
                        <p:inputText autocomplete="off"  styleClass="numericTxt" 
                                     id="tmp" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.stringValue}"  />


                        <p:commandButton id="btnAdd" value="Add Item" 
                                        action="#{storePurchaseController.addItem}" 
                                         process="cmbParent itemselectgrid" update="itemList itemselectgrid tot focusItem :#{p:component('total')}"/>

                    </p:panelGrid>

                    <h:panelGroup id="panelInventoryItemDetails" >

                        <p:panelGrid styleClass="alignTop"  columns="7" rendered="#{storePurchaseController.currentBillItem.item.departmentType eq 'Inventry'}" >

                            <h:outputLabel value="Parent Item"/>
                            <h:outputLabel value="Make"/>
                            <h:outputLabel value="Model"/>
                            <h:outputLabel value="Inventory Code"></h:outputLabel>
                            <h:outputLabel value="Description"></h:outputLabel>
                            <h:outputLabel value="Barcode No"/>
                            <h:outputLabel value="Serial No"/>


                            <p:selectOneMenu id="cmbParent" value="#{storePurchaseController.parentBillItem}" converter="temBillItemConverter"  >
                                <f:selectItem itemLabel="Select Parent Item" />
                                <f:selectItems   value="#{storePurchaseController.billItems}" var="bi" itemValue="#{bi}" itemLabel="#{bi.item.name}" />
                                <f:ajax event="change" execute="@this" /> 
                            </p:selectOneMenu>

                            <p:selectOneMenu id="cmbMake" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.make}" >
                                <f:selectItem itemLabel="Select a Make" ></f:selectItem>
                                <f:selectItems value="#{makeController.items}" var="make" itemLabel="#{make.name}" itemValue="#{make}" ></f:selectItems>
                            </p:selectOneMenu>
                            <p:inputText value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.model}" ></p:inputText>
                            <p:inputText id="txtInventoryCode" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.code}" ></p:inputText>
                            <p:inputTextarea id="txtInventoryDescription" 
                                             value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.description}" >
                            </p:inputTextarea>
                            <p:inputText id="txtBarcode" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.barcode}" ></p:inputText>
                            <p:inputText id="txtSerialNo" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.serialNo}" ></p:inputText>


                            <h:outputLabel value="Registration No"/>
                            <h:outputLabel value="Chassis No"/>
                            <h:outputLabel value="Engine No"/>
                            <h:outputLabel value="Colour"></h:outputLabel>
                            <h:outputLabel value="Warranty Certification No"></h:outputLabel>
                            <h:outputLabel value="Warranty Duration"/>
                            <h:outputLabel value="Depreciation Rate"/>


                            <p:inputText id="txtRegNo" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.registrationNo}" ></p:inputText>
                            <p:inputText id="txtChassisNo" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.chassisNo}" ></p:inputText>
                            <p:inputText id="txtEngineNo" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.engineNo}" ></p:inputText>
                            <p:inputText id="txtColour" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.colour}" ></p:inputText>
                            <p:inputText id="txtWarrentyCertificateNo" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.warrentyCertificateNumber}" ></p:inputText>
                            <p:outputLabel id="lblWarrantyDuration" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.warrentyDuration}" ></p:outputLabel>
                            <p:inputText id="txtDepreciationRate" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.deprecitionRate}" ></p:inputText>


                            <h:outputLabel value="Manufacturer"/>
                            <h:outputLabel value="Other Notes"/>
                            <h:outputLabel value=""/>
                            <h:outputLabel value=""></h:outputLabel>
                            <h:outputLabel value=""></h:outputLabel>
                            <h:outputLabel value=""/>
                            <h:outputLabel value=""/>

                            
                            <p:autoComplete id="acManufacturer" value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.manufacturer}"
                                            completeMethod="#{manufacturerController.completeManu}" 
                                            var="manufacturer"
                                            itemLabel="#{manufacturer.name}"
                                            itemValue="#{manufacturer}" ></p:autoComplete>
                            <p:inputTextarea id="txtOtherNotes" 
                                             value="#{storePurchaseController.currentBillItem.pharmaceuticalBillItem.otherNotes}" >
                            </p:inputTextarea>



                        </p:panelGrid>


                    </h:panelGroup>
                
                </h:panelGrid>

                <p:dataTable styleClass="noBorder" var="ph" value="#{storePurchaseController.billItems}" id="itemList" 
                             rowKey="#{ph.searialNo}">  
                    <f:facet name="header">  
                    </f:facet>  

                    <p:column headerText="Serial No" style="width:30%">  
                        <h:outputLabel id="serialNo" value="#{ph.searialNo}" style="width:100%">
                        </h:outputLabel>
                    </p:column>  

                    <p:column headerText="Item Name" style="width:30%">  
                        <h:outputLabel id="item" value="#{ph.item.name} - #{ph.item.code}" style="width:100%">
                        </h:outputLabel>
                    </p:column>  

                    <p:column headerText="Parent Item Name" style="width:30%">  

                        <h:outputLabel id="parentitem" value="#{ph.parentBillItem.item.name} - #{ph.parentBillItem.item.code}" style="width:100%">
                        </h:outputLabel>

                    </p:column>

                    <!--                    <p:column headerText="Qty" style="width:20%" >                     
                                            <p:inputText autocomplete="off" id="qty" value="#{ph.tmpQty}"  >
                                                <f:ajax event="blur" render="item qty freeQty purchase retail total :#{p:component('tot')}"
                                                        execute="item qty freeQty purchase retail" listener="#{storePurchaseController.onEdit(ph)}" ></f:ajax> 
                                            </p:inputText>
                                        </p:column>  
                    -->
                    <p:column headerText="Qty" style="width:20%">                     
                        <h:outputLabel id="qty" value="#{ph.tmpQty}"  />
                    </p:column> 

                    <!--                    <p:column headerText="Free" style="width:20%">       
                                            <p:inputText autocomplete="off" id="freeQty" value="#{ph.pharmaceuticalBillItem.freeQty}" style="width:100%" >
                                                <f:ajax event="blur" render="item qty freeQty purchase retail total :#{p:component('tot')}"
                                                        execute="item qty freeQty purchase retail" listener="#{storePurchaseController.onEdit(ph)}" ></f:ajax> 
                                            </p:inputText>
                                        </p:column> -->

                    <p:column headerText="Free" style="width:20%">       
                        <h:outputLabel id="freeQty" value="#{ph.pharmaceuticalBillItem.freeQty}" style="width:100%" />

                    </p:column> 

                    <!--                    <p:column headerText="P. Rate" style="width:24% ;">                     
                                            <h:inputText autocomplete="off" id="purchase" value="#{ph.pharmaceuticalBillItem.purchaseRate}">
                                                <f:ajax event="blur" render="profMargin @this retail total :#{p:component('tot')}"  execute="@this qty item qty freeQty purchase retail" listener="#{storePurchaseController.onEditPurchaseRate(ph)}" ></f:ajax> 
                                            </h:inputText>
                                            <p:outputLabel value=" X #{ph.item.dblValue}" rendered="#{ph.item.class eq 'class com.divudi.entity.pharmacy.Ampp'}"/>                                                
                                        </p:column>              -->

                    <p:column headerText="P. Rate" style="width:24% ;">                     
                        <h:outputLabel id="purchase" value="#{ph.pharmaceuticalBillItem.purchaseRate}"/>
                    </p:column>         

                    <!--                    <p:column headerText="S. Rate" style="width:24%">                   
                                            <h:inputText autocomplete="off" id="retail" value="#{ph.pharmaceuticalBillItem.retailRate}"  label="Price">
                                                <f:ajax event="blur" render="profMargin @this total :#{p:component('tot')}"  execute="item qty freeQty purchase retail" listener="#{storePurchaseController.onEdit(ph)}" ></f:ajax> 
                                            </h:inputText>
                                            <p:outputLabel value=" X (#{ph.item.dblValue})" rendered="#{ph.item.class eq 'class com.divudi.entity.pharmacy.Ampp'}"/>                    
                                        </p:column>  -->

                    <p:column headerText="S. Rate" style="width:24%">                   
                        <h:outputLabel  id="retail" value="#{ph.pharmaceuticalBillItem.retailRate}" />
                    </p:column>  

                    <p:column headerText="Value" style="width:20%;font-weight: bold;">  
                        <h:outputText id="total" value=" #{ph.pharmaceuticalBillItem.purchaseRate*ph.pharmaceuticalBillItem.qty}" />  

                    </p:column>  


                    <p:column headerText="Batch No/Serial Number" style="width:24%">                    
                        <h:outputText id="batNO" value=" #{ph.pharmaceuticalBillItem.stringValue}" />  
                    </p:column>


                    <p:column headerText="Profit Margin" style="width:24%">                    
                        <h:outputText id="profMargin" value=" #{(ph.pharmaceuticalBillItem.retailRate - ph.pharmaceuticalBillItem.purchaseRate ) / 100}" />  
                    </p:column>


                    <p:column>
                        <p:commandButton process="itemList cmdRemove"  update="itemList" id="cmdRemove" value="Remove" action="#{storePurchaseController.remove(ph)}">
                        </p:commandButton>
                    </p:column>
                </p:dataTable> 
                
                <p:panelGrid columns="2">
                
                <p:panelGrid columns="4">
                    
                    <p:panelGrid columns="2">
                        <p:outputLabel value="Invoice No : "/>
                        <p:inputText autocomplete="off" value="#{storePurchaseController.bill.invoiceNumber}" />
                        <p:outputLabel value="Invoice Date"/>
                        <p:calendar  value="#{storePurchaseController.bill.invoiceDate}"   navigator="true" pattern="dd/MM/yy hh:mm:ss" /> 
                        <h:outputLabel value="Qutation Number"/>
                        <p:inputText value="#{storePurchaseController.bill.qutationNumber}"/>                    
                    </p:panelGrid>
                    
                </p:panelGrid>


                <p:panel header="Bill Expenses" >
                    <p:focus id="focusExpense" for="acExpense" ></p:focus>
                    <h:panelGrid id="billExpenseGrid" columns="4" style="min-width: 100%;">
                        <h:outputLabel value="Select Expense"/>
                        <h:outputLabel value="Value"/>
                        <h:outputLabel value="Descreption"/>
                        <h:outputLabel ></h:outputLabel>
                        <p:autoComplete id="acExpense"   value="#{storePurchaseController.currentExpense.item}" 
                                        forceSelection="true"
                                        completeMethod="#{itemController.completeExpenseItem}" 
                                        var="ex" itemLabel="#{ex.name}" itemValue="#{ex}" >
                        </p:autoComplete>
                        <p:inputText autocomplete="off" id="txtExpense"
                                     styleClass="numericTxt" 
                                     value="#{storePurchaseController.currentExpense.rate}" style="width:100%" />  
                        <p:inputText maxlength="250" value="#{storePurchaseController.currentExpense.descreption}" ></p:inputText>
                        <p:commandButton id="btnAddExpense" value="Add Expense" 
                                        action="#{storePurchaseController.addExpense()}" 
                                         process="billExpenseGrid" 
                                         update="tblExpenses billExpenseGrid tot focusExpense :#{p:component('net')} :#{p:component('lblBillExpenses')}"/>
                    </h:panelGrid>
                    <p:dataTable id="tblExpenses" value="#{storePurchaseController.billExpenses}" var="be"
                                 emptyMessage="No Bill Expenses" >
                        <p:column headerText="Expense" >
                            <h:outputLabel value="#{be.item.name}" ></h:outputLabel>
                        </p:column>
                        <p:column headerText="Value" >
                            <h:outputLabel value="#{be.netValue}" >
                                <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                            </h:outputLabel>
                        </p:column>
                        <p:column headerText="Descreption" >
                            <h:outputLabel value="#{be.descreption}" ></h:outputLabel>
                        </p:column>
                    </p:dataTable>
                </p:panel>
                </p:panelGrid>

                <p:panel>
                    <p:panelGrid columns="2" id="tot" style="min-width: 100%">
                        <p:outputLabel value="Gross Total"/>
                        <p:outputLabel id="gro"  value="#{storePurchaseController.bill.total}" /> 
                        <p:outputLabel value="Tax"/>
                        <p:inputText autocomplete="off" id="tx" value="#{storePurchaseController.bill.tax}" > 
                            <p:ajax process="gro tx dis" update="net" event="keyup"/>
                        </p:inputText>
                        <p:outputLabel value="Expenses"/>
                        <p:outputLabel id="lblBillExpenses" 
                                       value="#{storePurchaseController.bill.expenseTotal}" > 
                        </p:outputLabel>
                        <p:outputLabel value="Discount"/>
                        <p:inputText autocomplete="off" id="dis" value="#{storePurchaseController.bill.discount}" >
                            <p:ajax process="gro tx dis" update="net" event="keyup"/>
                        </p:inputText>
                        <p:outputLabel value="Net Total"/>
                        <h:outputLabel id="net"  value="#{storePurchaseController.bill.netTotal}" > 
                            <f:convertNumber pattern="#,##0.00"/> 
                        </h:outputLabel>
                    </p:panelGrid>
                </p:panel>
            </p:panel>

            <p:panel rendered="#{storePurchaseController.printPreview}">
                <p:commandButton ajax="false" action="#{storePurchaseController.makeNull()}" value="New Bill"/>                    
                <p:commandButton value="Print" ajax="false" action="#" >
                    <p:printer target="gpBillPreview" ></p:printer>
                </p:commandButton>
                <p:panel id="gpBillPreview">
                    <stores:purhcase bill="#{storePurchaseController.bill}"/>
                </p:panel>
            </p:panel>
        </h:form>
    </ui:define>  

</ui:composition>
